package com.europe.business.europebusiness.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;

import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.adapter.viewholder.ListViewHolder;
import com.europe.business.europebusiness.ui.bean.Sector2Bean;
import com.europe.business.europebusiness.ui.common.Commons;
import com.europe.business.europebusiness.ui.net.ICompany;
import com.europe.business.europebusiness.ui.utils.Utils;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class Sector2Activity extends ListActivity1<Sector2Bean, Sector2Bean.Data> {

    private String sector1dataId;
    @Override
    protected void initView() {
        super.initView();
        setTitle(getString(R.string.info_select_sector2));
        setToolbarNavigationIcon(R.drawable.actionbar_back);
        sector1dataId = getIntent().getStringExtra("sector1dataId");
    }

    @Override
    protected boolean isInitToGetData() {
        return true;
    }



    @Override
    protected void bindView(ListViewHolder holder, Sector2Bean.Data o) {
        TextView tv = holder.getView(R.id.tv);
        if(tv != null){
            tv.setText(o.getSector2name());
        }
    }

    @Override
    protected void getData(final int pageNum, int pageSize) {
        if(pageNum == 1){
            showProgressDialog();
        }
        OkHttpClient client = new OkHttpClient.Builder().
                connectTimeout(60, TimeUnit.SECONDS).
                readTimeout(60, TimeUnit.SECONDS).
                writeTimeout(60, TimeUnit.SECONDS).build();
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Commons.SERVER_URL)
                .client(client)
                .build();
        ICompany company = retrofit.create(ICompany.class);
        Call<Sector2Bean> call = company.sector2List(sector1dataId, pageNum, pageSize, Utils.getSystemLanguage(this));
        call.enqueue(new Callback<Sector2Bean>() {
            @Override
            public void onResponse(Call<Sector2Bean> call, Response<Sector2Bean> response) {

                dismissProgressDialog();
                Sector2Bean bean = response.body();
                if(pageNum == 1){//第一页，一定是在下拉刷新
                    refrshRecycleViewDownPullOnResponse(bean);
                }else{//第二页，一定是在上拉加载更多
                    refrshRecycleViewUpPullOnResponse(bean);
                }
            }

            @Override
            public void onFailure(Call<Sector2Bean> call, Throwable t) {
                Log.e("ListActivity", t.getMessage());
                dismissProgressDialog();
                updateOnFailure();
            }
        });
    }

    @Override
    public void onItemClicked(int index) {
        Sector2Bean.Data data = listData.get(index);
        Intent intent = new Intent();
        intent.putExtra("data", data);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}
